package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.html.VueFormResult;
import m.html.element.Element;
import m.html.form.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.MenuInfo;
import manage.model.ModuleInfo;
import manage.service.ModuleService;

@ActionMeta(name="manage/module")
public class ModuleAction extends ManageAction {
    private ModuleInfo module;
    private MenuInfo menu;

    public String getPowerName() {
        return "manage_api_power";
    }
    public JSONMessage getModuleList() throws Exception {
        verifyAdminOperPower(getPowerName());
        return new JSONMessage(0)
                .push("list", ModelQueryList.getModelList(ModuleInfo.class,new String[]{"*"},null,null, QueryOrder.asc("sort")));
    }
    @RequestMeta(params = {"oid"})
    public JSONMessage getMenuList(String oid) throws Exception {
        verifyAdminOperPower(getPowerName());
        return new JSONMessage(0)
            .push("list", ModelQueryList.getModelList(MenuInfo.class,new String[]{"*"},null,
                QueryCondition.or(
                    QueryCondition.and(
                        QueryCondition.eq("moduleInfo.oid",oid),
                        QueryCondition.isEmpty("parentMenu.oid")
                    ),
                    QueryCondition.eq("parentMenu.oid",oid)
                ),
                QueryOrder.asc("sort")
            ));
    }
    public JSONMessage doSaveModule() throws Exception {
        systemLog("菜单","保存菜单模块信息");
        verifyAdminOperPower(getPowerName());
        String msg=getService(ModuleService.class).saveModule(module);
        return new JSONMessage(0,msg);
    }
    public VueResult toEditModule() throws Exception {
        verifyAdminOperPower(getPowerName());
        if(null!=module&&!StringUtil.isSpace(module.getOid())){
            module=ModelQueryList.getModel(module,"*");
        }else{
            module=new ModuleInfo();
            module.setIsInput("Y");
            module.setIsPublic("N");
            module.setType("A");
        }
        ModelForm form = new ModelForm("模块信息");
        form.addItem(
            FormInput.radio("类型", "model.type", "manage.model.ModuleInfo!type").setSpan(3).setPlaceholder("请选择类型"),
            FormInput.input("名称", "model.name").setPlaceholder("请输入名称"),
            FormHtml.ins("图标样式", Element.tag("a-space").E(
                FormHtml.getSelectButton("selectIconClass(model,'icoStyle')"),
                Element.tag("m-icon").A(":name","model.icoStyle"),
                FormHtml.getClearButton("model.icoStyle='';").A("v-if","model.icoStyle")
            )),
            FormInput.number("排序", "model.sort").setPlaceholder("请输入排序"),
            FormInput.input("地址", "model.urlPath").setPlaceholder("请输入地址").setSpan(3)
        )
        .addData("model",module)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSaveModule"),"{module:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    public JSONMessage doSaveMenu() throws Exception {
        systemLog("菜单","保存菜单信息");
        verifyAdminOperPower(getPowerName());
        String msg=getService(ModuleService.class).saveMenu(menu);
        return new JSONMessage(0,msg);
    }
    public VueResult toEditMenu() throws Exception {
        verifyAdminOperPower(getPowerName());
        if(null!=menu&&!StringUtil.isSpace(menu.getOid())){
            menu=ModelQueryList.getModel(menu,"*");
        }else if(null!=menu&&(
                null!=menu.getModuleInfo()&&!StringUtil.isSpace(menu.getModuleInfo().getOid())
                ||null!=menu.getParentMenu()&&!StringUtil.isSpace(menu.getParentMenu().getOid())
            )){
            menu.setIsInput("Y");
            menu.setIsPublic("N");
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        ModelForm form = new ModelForm("菜单信息");
        form.addItem(
            FormInput.input("名称", "model.name").setPlaceholder("请输入名称"),
            FormHtml.ins("图标样式", Element.tag("a-space").E(
                FormHtml.getSelectButton("selectIconClass(model,'icoStyle')"),
                Element.tag("m-icon").A(":name","model.icoStyle"),
                FormHtml.getClearButton("model.icoStyle='';").A("v-if","model.icoStyle")
            )),
            FormInput.number("排序", "model.sort").setPlaceholder("请输入排序"),
            FormInput.input("地址", "model.urlPath").setSpan(3).setPlaceholder("请输入地址"),
            FormInput.input("描述", "model.description").setSpan(3).setPlaceholder("请输入描述")
        )
        .addData("model",menu)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSaveMenu"),"{menu:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }

    public ModuleInfo getModule() {
        return module;
    }

    public void setModule(ModuleInfo module) {
        this.module = module;
    }

    public MenuInfo getMenu() {
        return menu;
    }

    public void setMenu(MenuInfo menu) {
        this.menu = menu;
    }
}
